package gemeente.meldStad;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("unchecked")
public class AllItemsOverlay extends ItemizedOverlay {
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private boolean clickable = true;
	private boolean isPinch = false;
	
	public AllItemsOverlay(Drawable defaultMarker, Context context) {
		//boundcenterBottom() -> midden-onder van marker als locatie 
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}
	
	public AllItemsOverlay(Drawable defaultMarker, Context context, boolean click) {
		//boundcenterBottom() -> midden-onder van marker als locatie 
		super(boundCenterBottom(defaultMarker));
		mContext = context;
		clickable = click;
	}

	public void addOverlay(OverlayItem overlay) {
	    //add item to item-arraylist
		mOverlays.add(overlay);  
	}
	
	public void doPopulate(){
		//populate calls createItem
	    populate();
	}
	
	public void clear(){
		mOverlays.clear();
	}
	
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		return mOverlays.size();
	}
	
	@Override
	protected boolean onTap(int index) {
		if ( isPinch )
	    {
	        return false;
	    }
	    else if(clickable == true){
		  final CustomOverlayItem item = (CustomOverlayItem) mOverlays.get(index);
		  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext)
          .setTitle(item.getTitle())
          .setMessage(item.getSnippet())
          .setPositiveButton("Terug",new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
              }
          })
          .setNegativeButton("Meer Informatie", new DialogInterface.OnClickListener() {
              public void onClick(DialogInterface dialog, int whichButton) {
            	  Intent newIntent = new Intent(mContext, MeldingOverzichtActivity.class);
            	  newIntent.putExtra("id" , item.getId());	
            	  newIntent.putExtra("bevestigen" , false);
            	  mContext.startActivity(newIntent);
              }
          }); 
		  
		  dialog.show();
		}
	  return true;
	}
	
	public boolean onTouchEvent(MotionEvent e, MapView mapView)
	{
	    int fingers = e.getPointerCount();
	    if ( e.getAction()==0 ) isPinch=false;                  // touch down, don't know it's a pinch yet
	    if ( e.getAction()==2 && fingers==2 ) isPinch=true;     // Two fingers, def a pinch
	    return super.onTouchEvent(e,mapView);
	}
}
